package com.jiyejia.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import com.jiyejia.R;
import com.jiyejia.model.Order;

import java.util.ArrayList;

/**
 * Created by zhoucong on 13-6-17.
 */
public class ListAdapter extends BaseAdapter {
    public static String tag = "ListAdapter";
    ArrayList<Order> arrayList;
    private Context c;
    public ListAdapter(ArrayList<Order> list, Context c){
        arrayList = list;
        this.c = c;

    }
    public void setData(ArrayList<Order> list){
    	this.arrayList = list;
    }

    @Override
    public int getCount() {
        return arrayList.size();
    }

    @Override
    public Object getItem(int i) {
        return arrayList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        Holder holder = new Holder();
        View convertView = view;
        if(arrayList==null||arrayList.size()==0)return null;
        Order order = arrayList.get(i);
        if(convertView==null){
            convertView = LayoutInflater.from(c).inflate(R.layout.item_order,null);
            holder.tv = (TextView)convertView.findViewById(R.id.item_name);
            holder.button = (Button)convertView.findViewById(R.id.item_add);
            convertView.setTag(holder);
        }else{
            holder = (Holder)convertView.getTag();
        }
        if(order!=null&&order.mOrderName!=null){
            holder.tv.setText(order.mOrderName);
        }else{
            Log.e(tag,"Order is not avalable");
        }
        return convertView;
    }
    public static class Holder{
        TextView tv ;
        Button button;
    }
}
